/**************************************************************************
 * @file usr_lcd.h
 * @author 黑马小乌龟 (532352406@qq.com)
 * @brief 
 * @version 0.1
 * @date 2025-08-15
 * 
 * @copyright Copyright (c) 2025
 * 
 *************************************************************************/
#ifndef USR_LCD__H_H
#define USR_LCD__H_H

#ifdef __cplusplus
extern "C"{
#endif
#include <stdint.h>
#include <stdio.h>

#include "ev_hc32f334_lqfp64_bsp.h"
#include "hc32_ll.h"
#include "hc32_ll_spi.h"
#include "hc32_ll_dma.h"
/* unlock/lock peripheral */
#define EXAMPLE_PERIPH_WE               (LL_PERIPH_GPIO | LL_PERIPH_EFM | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU)
#define EXAMPLE_PERIPH_WP               (LL_PERIPH_EFM | LL_PERIPH_FCG)

/* Configuration for Example */
#define EXAMPLE_SPI_MASTER_SLAVE        (SPI_MASTER)
#define EXAMPLE_SPI_BUF_LEN             (128u)

/* SPI definition */
#define SPI_UNIT                        (CM_SPI)
#define SPI_CLK                         (FCG1_PERIPH_SPI)
#define SPI_TX_EVT_SRC                  (EVT_SRC_SPI_SPTI)
#define SPI_RX_EVT_SRC                  (EVT_SRC_SPI_SPRI)

/* DMA definition */
#define DMA_UNIT                        (CM_DMA)
#define DMA_CLK                         (FCG0_PERIPH_DMA | FCG0_PERIPH_AOS)
#define DMA_TX_CH                       (DMA_CH2)
#define DMA_TX_TRIG_CH                  (AOS_DMA_2)
#define DMA_TX_INT_SRC                  (INT_SRC_DMA_TC2)
#define DMA_TX_INT_CH                   (DMA_INT_TC_CH2)
#define DMA_TX_IRQ_NUM                  (INT006_IRQn)//(DMA_TC2_BTC2_IRQn)

#define DMA_RX_CH                       (DMA_CH1)
#define DMA_RX_INT_CH                   (DMA_INT_TC_CH1)
#define DMA_RX_TRIG_CH                  (AOS_DMA_1)
#define DMA_RX_INT_SRC                  (INT_SRC_DMA_TC1)
#define DMA_RX_IRQ_NUM                  (INT006_IRQn)

/* SS = PB7 */
#define SPI_SS_PORT                     (GPIO_PORT_B)
#define SPI_SS_PIN                      (GPIO_PIN_03)
#define SPI_SS_FUNC                     (GPIO_FUNC_48)
/* SCK = PB5 */
#define SPI_SCK_PORT                    (GPIO_PORT_B)
#define SPI_SCK_PIN                     (GPIO_PIN_07)
#define SPI_SCK_FUNC                    (GPIO_FUNC_49)
/* MOSI = PA0 */
#define SPI_MOSI_PORT                   (GPIO_PORT_B)
#define SPI_MOSI_PIN                    (GPIO_PIN_06)
#define SPI_MOSI_FUNC                   (GPIO_FUNC_50)
/* MISO = PA1 */
// #define SPI_MISO_PORT                   (GPIO_PORT_A)
// #define SPI_MISO_PIN                    (GPIO_PIN_01)
// #define SPI_MISO_FUNC                   (GPIO_FUNC_51)
typedef struct ascii_font_infor {
    uint8_t font_width;
    uint8_t font_height;
    const uint8_t *p_font_table;
} ASCII_FONT_INFOR_T;

/* SPI communication mode */
#define SPI_COMM_MD                     (SPI_COMM_MD_NORMAL)

#define LCD_RES_CTL_PORT (GPIO_PORT_B)
#define LCD_RES_CTL_PIN  (GPIO_PIN_05)
#define LCD_A0_CTL_PORT  (GPIO_PORT_B)
#define LCD_A0_CTL_PIN   (GPIO_PIN_04)

#define LCD_MAX_BUF_LINE      8u
#define LCD_ONE_LINE_MAX_BYTE 128u

#define LCD_RES_CTL(level) ((level) ? GPIO_SetPins(LCD_RES_CTL_PORT, LCD_RES_CTL_PIN) : GPIO_ResetPins(LCD_RES_CTL_PORT, LCD_RES_CTL_PIN))
#define LCD_A0_CTL(level)  ((level) ? GPIO_SetPins(LCD_A0_CTL_PORT, LCD_A0_CTL_PIN) : GPIO_ResetPins(LCD_A0_CTL_PORT, LCD_A0_CTL_PIN))

extern const ASCII_FONT_INFOR_T ascii_32x32_font;
extern const ASCII_FONT_INFOR_T ascii_16x16_font;
extern const ASCII_FONT_INFOR_T ascii_24x24_font;
extern const ASCII_FONT_INFOR_T ascii_16x16_font_rev;
extern const ASCII_FONT_INFOR_T ascii_16x16_font_bold;

void LCD_SPI_Config(void);
void lcd_test_fun(void);
void lcd_init(void);
void disp_col(void);
void DMA_spi_send_one_line_dat_start(uint8_t start_line_add, uint8_t *p_buf, uint16_t buf_len);
void usr_lcd_flush_start(void);
void usr_lcd_flush_next_line(void);
void lcd_ascii_str_dis(uint8_t row, uint8_t column,const ASCII_FONT_INFOR_T font_infor, const char *p_str);

#ifdef __cplusplus
}
#endif

#endif //USR_LCD__H_H


